package com.cpit.platform.common.utils;

import org.springframework.security.core.context.SecurityContextHolder;

import com.cpit.platform.dto.OrganizationPair;
import com.cpit.platform.dto.User;
import com.cpit.platform.security.service.UserInfo;
import com.cpit.platform.service.impl.OrganizationServiceImpl;

public class AuthUtils {

	public static OrganizationPair getCurrentOrganizationCode() throws Exception {
		OrganizationServiceImpl organizationService = SpringStaticHolder.getBean(OrganizationServiceImpl.class);
		UserInfo ui = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User usercur = ui.getUser();
		if(usercur == null) {
			throw new Exception("No USER login");
		}
		int oId = usercur.getOrganizationId();
		String oCode = organizationService.findCodeById(oId);
		OrganizationPair p = new OrganizationPair(oCode, usercur.getAuthType());
		return p;
	}
}
